package org.apache.kafka.niezhipeng.simpleDemo.partitioner;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;
import java.util.Random;

/**
 * @author Nie ZhiPeng
 * @date 2025/8/1 15:04
 */
public class RandomPartitioner implements Partitioner {
    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        int partitionNum = cluster.partitionsForTopic(topic).size();
        Random random = new Random();
        return random.nextInt(partitionNum);
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
